package com.sctvcloud.renhe.ui.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.ruihang.generalibrary.utils.SharedPreferencesUtil;
import com.ruihang.generalibrary.utils.UrlUtils;
import com.sctvcloud.renhe.R;
import com.sctvcloud.renhe.base.BaseActivity;
import com.sctvcloud.renhe.application.Constances;
import com.sctvcloud.renhe.beans.AdvertBean;
import com.sctvcloud.renhe.beans.ImageUr;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 浮动广告(dialog)
 */
public class FloatAdActivity extends Activity {
    private ImageView ivFloatView;
    private View vCloseView;
    private AdvertBean advertBean;
    private Timer timer;
    private TimerTask timerTask;
    private int time;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_float_ad);
        advertBean = (AdvertBean) getIntent().getSerializableExtra(BaseActivity.EX_DATA);
        if (advertBean == null) {
            finish();
            overridePendingTransition(0, 0);
            return;
        }
        //根据版本号 对当前点击次数+1
        int count = (int) SharedPreferencesUtil.getParam(this, String.valueOf(advertBean.getVersionCode()), 0);
        SharedPreferencesUtil.setParam(this, String.valueOf(advertBean.getVersionCode()), count++);
        ivFloatView = (ImageView) findViewById(R.id.iv_float_image);
        if (!TextUtils.isEmpty(advertBean.getImageUrl())) {
//            Glide.with(this).load(UrlUtils.linkUrls(Constances.ST_BASE_URL, advertBean.getImageUrl())).placeholder(R.mipmap.default_icon_1_1).error(R.mipmap.default_icon_1_1).into(ivFloatView);
            Glide.with(this).load(UrlUtils.linkUrls(Constances.ST_BASE_URL, advertBean.getImageUrl())).dontTransform().listener(new RequestListener<String, GlideDrawable>() {
                @Override
                public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                    finish();
                    return false;
                }

                @Override
                public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                    ivFloatView.setVisibility(View.VISIBLE);
//                    vCloseView.setVisibility(View.VISIBLE);
                    return false;
                }
            }).into(ivFloatView);

        }
        time =
                advertBean.getShowTime();
        if (time > 0) {
            timerTask = new TimerTask() {
                @Override
                public void run() {
                    if (time == 0) {
                        timerTask.cancel();
                        timer.cancel();
                        finish();
                        overridePendingTransition(0, 0);
                    } else {
                        time--;
                    }
                }
            };
            timer = new Timer();
            timer.schedule(timerTask, 0, 1000);
        }

        ivFloatView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                advertBean.setContentUrl("http://www.baidu.com");
                if(null!=advertBean.getNewsUrlList()&&advertBean.getNewsUrlList().size()>0){
                    String newsUrl = advertBean.getNewsUrlList().get(0).getNewsUrl();
                    if (!TextUtils.isEmpty(newsUrl)) {
                        Intent intent = new Intent(FloatAdActivity.this, NewesDetailActivity.class);
                        intent.putExtra(BaseActivity.EX_URL, newsUrl);
                        FloatAdActivity.this.startActivity(intent);
                        finish();
                        overridePendingTransition(0, 0);
                        return;
                    }
                }
                if (!TextUtils.isEmpty(advertBean.getContentUrl())) {
                    Intent intent = new Intent(FloatAdActivity.this, H5NormalActivity.class);
                    intent.putExtra(BaseActivity.EX_HTML, UrlUtils.linkUrls(Constances.ST_BASE_URL, advertBean.getContentUrl()));
                    startActivity(intent);
                    finish();
                    overridePendingTransition(0, 0);
                }

            }
        });
        vCloseView = findViewById(R.id.iv_close);
        vCloseView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                overridePendingTransition(0, 0);
            }
        });
        findViewById(R.id.rl_root).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                overridePendingTransition(0, 0);
            }
        });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(0, 0);
    }
}
